package com.ask.service.Impl;

import com.ask.constant.StatusConstant;
import com.ask.dto.CategoryDTO;
import com.ask.dto.CategoryQueryDTO;
import com.ask.entity.Category;
import com.ask.exception.ResourceNotExistException;
import com.ask.mapper.CategoryMapper;
import com.ask.result.PageResult;
import com.ask.result.Result;
import com.ask.service.CategorySerivce;
import com.fasterxml.jackson.databind.ser.std.NumberSerializers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategorySerivceImpl implements CategorySerivce {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 修改分类
     *
     * @param categoryDTO
     * @return
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        // todo aop
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(1l);
        categoryMapper.update(category);
    }

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        // todo aop
        category.setCreateTime(LocalDateTime.now());
        category.setCreateUser(1l);
        category.setStatus(StatusConstant.DISABLE);
        categoryMapper.insert(category);
    }

    /**
     * 启用、禁用分类
     *
     * @param status
     * @return
     */
    @Override
    public void enableOrDisable(Integer status, Long id) {
        Category category = categoryMapper.getById(id);
        if (category == null)
            throw new ResourceNotExistException("分类不存在！");
        category.setStatus(status);
        categoryMapper.update(category);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> getByType(Integer type) {
        Category category = new Category();
        category.setType(type);
        return categoryMapper.list(category);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @Override
    public void delete(Long id) {
        // todo update with table dish
        categoryMapper.deleteById(id);
    }

    @Override
    public PageResult pageQuery(CategoryQueryDTO categoryQueryDTO) {
        PageHelper.startPage(categoryQueryDTO.getPage(), categoryQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Category> list(Integer type) {
        Category category = new Category();
        category.setType(type);
        return categoryMapper.list(category);
    }
}
